package com.gosophia.govDoc.action.queryDoc;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.gosophia.commons.DateTranslate;
import com.gosophia.govDoc.common.DocumentJobType;
import com.gosophia.govDoc.common.TurningAction;
import com.gosophia.govDoc.dto.DocumentTaskDTO;
import com.gosophia.govDoc.ws.DocumentQueryWebService;
import com.gosophia.taskManagement.action.BaseMissionDetailAction;
import com.gosophia.taskManagement.dto.JobHandleRecordsDTO;
import com.gosophia.taskManagement.dto.MissionDTO;
import com.gosophia.taskManagement.ws.TaskManagementWebService;

@Scope("prototype")
@Controller("GetDocMissionDetailAction")
public class GetDocMissionDetailAction extends BaseMissionDetailAction{

	private static final long serialVersionUID = -6143145314921415161L;
	private String editType;
	
	@Autowired
	private DocumentQueryWebService documentQueryWebService;
	@Autowired
    protected TaskManagementWebService taskManagementWebService;
	private Long jobId;
    
	private DocumentTaskDTO documentTaskDTO;
	
	private List<JobHandleRecordsDTO> historyList = new ArrayList<JobHandleRecordsDTO>();

	@Override
	public String _execute() throws Exception {
	    editType="0,0";
		documentTaskDTO = documentQueryWebService.findDocumentJobDetail(this.getMissionId());
		documentTaskDTO.setVersion("LATEST_VERSION");
		List<JobHandleRecordsDTO> hislist;
		hislist = super.getHistoryList();
		if(hislist!=null&&hislist.size()!=0) {
			for(int i = 0;i<hislist.size();i++) {
				if(DocumentJobType.simulate.toString().equals(hislist.get(i).getJobType())) {
					    DocumentTaskDTO dcoTaskDto = documentQueryWebService.findDocProcessDetail(hislist.get(i).getJobId());
					    StringBuffer content = new StringBuffer("起草了");
			            content.append("["+documentTaskDTO.getMissionName()+"]");
			            hislist.get(i).setContent(content.toString());
			            historyList.add(hislist.get(i));
			              
			            JobHandleRecordsDTO missionCreateRecord1 = new JobHandleRecordsDTO();
			            missionCreateRecord1.setActualEndTime(hislist.get(i).getActualEndTime());
			            missionCreateRecord1.setOperatorName(hislist.get(i).getOperatorName());
			            missionCreateRecord1.setDetailURL(hislist.get(i).getDetailURL());
			            missionCreateRecord1.setJobId(hislist.get(i).getJobId());
			            missionCreateRecord1.setLevel(hislist.get(i).getLevel());
			            StringBuffer content1 = new StringBuffer("提交给");
			            content1.append(" "+dcoTaskDto.getUndertakerName()+" ");
			            content1.append("进行审批");
			            missionCreateRecord1.setContent(content1.toString());
			            historyList.add(missionCreateRecord1);
				}
				if(DocumentJobType.approve.toString().equals(hislist.get(i).getJobType())) {
					DocumentTaskDTO dcoTaskDto = documentQueryWebService.findDocProcessDetail(hislist.get(i).getJobId());
					if(TurningAction.SUBMIT_NEXT_APPROVAL.toString().equals(dcoTaskDto.getTurningAction())) {
						StringBuffer content = new StringBuffer("同意并提交给");
				        content.append(" "+dcoTaskDto.getUndertakerName()+" ");
				        content.append("进行审批");
				        hislist.get(i).setContent(content.toString());
				        historyList.add(hislist.get(i));
					}
					if(TurningAction.AGREE_FINALIZE.toString().equals(dcoTaskDto.getTurningAction())) {
						StringBuffer content = new StringBuffer("同意定稿");
						hislist.get(i).setContent(content.toString());
						historyList.add(hislist.get(i));
					}
					if(TurningAction.SENDBACK.toString().equals(dcoTaskDto.getTurningAction())){
						MissionDTO missionDto = taskManagementWebService.findMissionDetailByMissionId(this.getMissionId());
						StringBuffer content = new StringBuffer("退回给了");
				        content.append(" "+missionDto.getFirstJobDTO().getOperatorDTO().getUserName()+" ");
				        hislist.get(i).setContent(content.toString());
				        historyList.add(hislist.get(i));
					}
				  
			    }
				if(DocumentJobType.getBack.toString().equals(hislist.get(i).getJobType())) {
					DocumentTaskDTO dcoTaskDto = documentQueryWebService.findDocProcessDetail(hislist.get(i).getJobId());
				    
					StringBuffer content = new StringBuffer("取回了");
		            content.append("["+documentTaskDTO.getMissionName()+"]");
		            hislist.get(i).setContent(content.toString());
		            historyList.add(hislist.get(i));
		            
		            JobHandleRecordsDTO missionCreateRecord2 = new JobHandleRecordsDTO();
		            missionCreateRecord2.setActualEndTime(hislist.get(i).getActualEndTime());
		            missionCreateRecord2.setOperatorName(hislist.get(i).getOperatorName());
		            missionCreateRecord2.setDetailURL(hislist.get(i).getDetailURL());
		            missionCreateRecord2.setJobId(hislist.get(i).getJobId());
		            missionCreateRecord2.setLevel(hislist.get(i).getLevel());
		            StringBuffer content1 = new StringBuffer("发给");
		            content1.append(" "+dcoTaskDto.getUndertakerName()+" ");
		            content1.append("进行审批");
		            missionCreateRecord2.setContent(content1.toString());
		            historyList.add(missionCreateRecord2);
				}
				if(DocumentJobType.modify.toString().equals(hislist.get(i).getJobType())) {
					DocumentTaskDTO dcoTaskDto = documentQueryWebService.findDocProcessDetail(hislist.get(i).getJobId());
					StringBuffer content = new StringBuffer("修订了");
		            content.append("["+documentTaskDTO.getMissionName()+"]");
		            content.append("提交给");
		            content.append(" "+dcoTaskDto.getUndertakerName()+" ");
		            content.append("进行审批");
		            hislist.get(i).setContent(content.toString());
		            historyList.add(hislist.get(i));
				}
				if(DocumentJobType.documentFinalize.toString().equals(hislist.get(i).getJobType())) {
					StringBuffer content = new StringBuffer("定稿");
					hislist.get(i).setContent(content.toString());
					historyList.add(hislist.get(i));
				}
				//获取最后文档的最新版本 
				
			}
			historyList.add(0,hislist.get(0));
		}
				return SUCCESS;
	}

	public Long getJobId() {
		return jobId;
	}

	public void setJobId(Long jobId) {
		this.jobId = jobId;
	}

	public DocumentTaskDTO getDocumentTaskDTO() {
		return documentTaskDTO;
	}

	public void setDocumentTaskDTO(DocumentTaskDTO documentTaskDTO) {
		this.documentTaskDTO = documentTaskDTO;
	}

	@Override
    public List<JobHandleRecordsDTO> getHistoryList() {
		return historyList;
	}

	@Override
    public void setHistoryList(List<JobHandleRecordsDTO> historyList) {
		this.historyList = historyList;
	}

    public String getEditType() {
        return editType;
    }

    public void setEditType(String editType) {
        this.editType = editType;
    }

	
	
}
